//
//  XKHomeCollectionViewLayout.m
//  xk100
//
//  Created by xiaokang on 2017/5/17.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "XKHomeCollectionViewLayout.h"

@interface XKHomeCollectionViewLayout ()
@property (nonatomic, assign) CGFloat totalHeight;
@property (nonatomic, strong) NSMutableArray *attrsArr;
@end
@implementation XKHomeCollectionViewLayout
-(void)prepareLayout {
    [super prepareLayout];
    //    NSLog(@"准备布局");
    self.totalHeight = 0;
    // 组布局
    NSMutableArray *attributesArr = [NSMutableArray array]; //
    NSInteger sectionCount = [self.collectionView numberOfSections]; // 组 数目
    for (int i = 0; i < sectionCount; i++) {
        
        // 组头布局
        NSIndexPath *indexP = [NSIndexPath indexPathWithIndex:i];
        UICollectionViewLayoutAttributes *attr = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexP];
        [attributesArr addObject:attr]; // 添加到布局数组
        
        // item布局
        NSInteger itemCount = [self.collectionView numberOfItemsInSection:i];
        for (int j = 0; j < itemCount; j++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
            [attributesArr addObject:attrs]; // 添加到布局数组
        }
        
        // 组尾部布局
        UICollectionViewLayoutAttributes *attr1 = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:indexP];
        [attributesArr addObject:attr1]; // 添加到布局数组
    }
    
    self.attrsArr = [NSMutableArray arrayWithArray:attributesArr];
}


/// collectionView 的 contentSize (最终的上下文尺寸)
-(CGSize)collectionViewContentSize {
    //    NSLog(@"返回collectionView的上下文尺寸");
    return CGSizeMake(self.collectionView.bounds.size.width, self.totalHeight);
}


// 组头/组尾布局
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewLayoutAttributes *layoutAttrs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    CGFloat height = 0;
    
    if (elementKind == UICollectionElementKindSectionHeader) {
        //        NSLog(@"开始组头布局");
        if (_delegate != nil && [_delegate respondsToSelector:@selector(heightOfSectionHeaderForIndexPath:)]) {
            height = [_delegate heightOfSectionHeaderForIndexPath:indexPath];
        }
    } else {
        //        NSLog(@"开始组尾布局");
        if (_delegate != nil && [_delegate respondsToSelector:@selector(heightOfSectionFooterForIndexPath:)]) {
            height = [_delegate heightOfSectionFooterForIndexPath:indexPath];
        }
    }
    layoutAttrs.frame = CGRectMake(0, self.totalHeight, Main_Width, height);
    self.totalHeight += height;
    return layoutAttrs;
}

// 布局数据
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    //    NSLog(@"返回布局数据数组");
    return self.attrsArr;
}


// item布局(根据组的不同分有四个样式)
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //    NSLog(@"开始item布局");
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    if (indexPath.section == 0) {
        [self layoutAttributesForFunctionCellLayout:layoutAttributes indexPath:indexPath];
    } else if(indexPath.section == 1) {
        [self layoutAttributesForNewsCellLayout:layoutAttributes indexPath:indexPath];
    }
    return layoutAttributes;
}

- (void)layoutAttributesForFunctionCellLayout:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath:(NSIndexPath *)indexPath {
    
    CGFloat y = self.totalHeight;
    CGFloat margin = 1*Scale_W;
    CGFloat col = (indexPath.item) % 4; // 列
    
    if (indexPath.item > 3) return;
    CGFloat width = (Main_Width - (4 - 1) * margin) / 4.0;
    layoutAttributes.frame = CGRectMake(col * (margin + width), y, width, width);
    if (indexPath.item == 3 || indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1) {
        self.totalHeight += width;
    }
}


- (void)layoutAttributesForNewsCellLayout:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath:(NSIndexPath *)indexPath {
    
    CGFloat col = (indexPath.item) % 2; // 列
//    CGFloat row = (indexPath.item) / 2; // 行

    if (indexPath.item > [self.collectionView numberOfItemsInSection:indexPath.section]-1) return;
    CGFloat height = 135*Scale_H;
    CGFloat width  = (Main_Width - 0) / 2.0;
    CGFloat x      = 0 + width * col;
    CGFloat y      = self.totalHeight;

    layoutAttributes.frame = CGRectMake(x, y, width, height);
    
    if (col == 1 || indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1) {
        self.totalHeight += height;
    }
}




@end
